package com.bailiban.aigou.admin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bailiban.aigou.admin.common.BaseAction;
import com.bailiban.aigou.admin.dao.CarouselFigureDao;
import com.bailiban.aigou.admin.entity.CarouselFigure;
import com.bailiban.aigou.admin.service.ICarouselFigureService;
import com.bailiban.aigou.admin.utils.PageModel;

@Controller
@Scope("prototype")
@Namespace("/admin/carouselfigure")
@Results({
    @Result(name = "error", location = "/WEB-INF/jsp/aigou/500.jsp"),
	@Result(name = "close", location = "/WEB-INF/jsp/aigou/success.jsp")
})
public class CarouselFigureAction extends BaseAction {
	
	 /**
	 * 轮播action
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private ICarouselFigureService CarouselFigureService;
	
    /**
     * 去轮播维护界面
     * @return
     */
	@Action(value="list",results={ @Result( name="success" ,location ="/WEB-INF/jsp/aigou/admin/carouselfigure/list.jsp")})
	 public String list(){
		System.out.println("轮播维护界面");
		    HttpServletRequest request = ServletActionContext.getRequest();
		    super.setPageNo(request.getParameter("pageNo"));
			super.setPageSize(request.getParameter("pageSize"));
			PageModel<CarouselFigure> CarouselFigurePages = CarouselFigureService.getCarouselFigurePages(super.getPageNo(), super.getPageSize());
			request.setAttribute("CarouselFigurePages", CarouselFigurePages);
		  
		
		return SUCCESS;
		 
	 }
	/**
	 * 去轮播图添加界面
	 * @return
	 */
	@Action(value="addPage",results={@Result(name=SUCCESS,location="/WEB-INF/jsp/aigou/admin/carouselfigure/add.jsp")})
	public String addPage(){
		
		return SUCCESS;
		
	}
	/**
	 * 添加轮播实体
	 * @return
	 */
	@Action(value="add")
	public String add(){
		    HttpServletRequest request = ServletActionContext.getRequest();
		    String url = request.getParameter("url");
			String sequenceNum = request.getParameter("sequenceNum");
			CarouselFigure carouselfigure = new CarouselFigure();
			
			/*carouselfigure.setId(UUIDUtils.getId());  不需要了 实体类使用了hibernate的注解自动生成uuid*/
			carouselfigure.setUrl(url);
			carouselfigure.setSequenceNum(Integer.parseInt(sequenceNum));
			
			CarouselFigureService.addCarouselFigure(carouselfigure);
			request.setAttribute("url", "admin/carouselfigure/list");
		return "close";
		
	}
	/**
	 * 去更新轮播实体界面
	 * @return
	 */
	@Action(value="updatePage",results={@Result(name=SUCCESS,location="/WEB-INF/jsp/aigou/admin/carouselfigure/update.jsp")})
	public String updatePage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		CarouselFigure carouselfigure = CarouselFigureService.getCarouselFigureById(id);
		request.setAttribute("carouselfigure", carouselfigure);
		return SUCCESS;
		
		
	}
	/**
	 * 更新轮播实体
	 * @return
	 */
	@Action(value="update")
	public String update(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		String url = request.getParameter("url");
		String sequenceNum = request.getParameter("sequenceNum");
		CarouselFigure carouselfigure = CarouselFigureService.getCarouselFigureById(id);
		carouselfigure.setUrl(url);
		carouselfigure.setSequenceNum(Integer.parseInt(sequenceNum));
		CarouselFigureService.updateCarouselFigure(carouselfigure);
		request.setAttribute("url", "admin/carouselfigure/list");
		return "close";
		
	}
	
	/**
	 * 删除轮播实体
	 * 
	 * @return
	 */
	@Action(value = "delete")
	public String delete() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		CarouselFigureService.deleteCarouselFigureById(id);
		request.setAttribute("url", "admin/carouselfigure/list");
		return "close";
    
	}
	

}
